﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SIMS.Global;
using SIMS.Database;
using System.Collections;
using System.Data;

namespace SIMS.School
{
    public class CourseDetail
    {
        public int Id { get; set; }
        public int CompanyID { get; set; }
        public string CompanyName { get; set; }

        public int CourseID { get; set; }
        public string CourseName { get; set; }

        public int StandardLevelID { get; set; }
        public string StandardLevelName { get; set; }

        public int MajorFieldID { get; set; }
        public string MajorFieldName { get; set; }

        public string Name { get; set; }
        public string Description { get; set; }

        public bool IsActive { get; set; }
        public DateTime CreatedDate { get; set; }
        public DateTime ModifiedDate { get; set; }

        public MyError Error { get; set; }
    }

    public class CourseDetailList : List<CourseDetail>
    {
        public MyError Error { get; set; }
        public CourseDetailList()
        {
        }
    }

    public class CourseDetailDB
    {
        MyDatabase _db = new MyDatabase();

        public CourseDetail Save(CourseDetail CourseDetail)
        {
            try
            {
                SortedList _param = new SortedList();
                _param.Add("@Id", CourseDetail.Id);
                _param.Add("@CourseId", CourseDetail.CourseID);
                _param.Add("@MajorFieldId", CourseDetail.MajorFieldID);
                _param.Add("@StandardLevelId", CourseDetail.StandardLevelID);

                if (CourseDetail.Description != string.Empty)
                {
                    _param.Add("@Description", CourseDetail.Description);
                }
                else
                {
                    _param.Add("@Description", DBNull.Value);
                }

                _param.Add("@Name", CourseDetail.Name);


                _param.Add("@IsActive", CourseDetail.IsActive);

                int _affRow = _db.ExecuteNonSqlSP("usp_CourseDetailInsertUpdateSingleItem", _param);

                if (_affRow > 0)
                {
                    CourseDetail.Error.ErrorNo = 0;
                }
                else
                {
                    CourseDetail.Error.ErrorNo = 1;
                    CourseDetail.Error.ErrorMessage = "No row affect when saving school's course details.";
                }
            }
            catch (Exception Ex)
            {
                CourseDetail.Error.ErrorNo = 10002;
                CourseDetail.Error.ErrorLocation = "Course";
                CourseDetail.Error.ErrorMessage = Ex.Message;
                CourseDetail.Error.ErrorStack = Ex.StackTrace;

                MyError.CreateExceptionLog(Ex, "Course");
            }

            return CourseDetail;
        }

        public CourseDetailList GetCourseDetailList(int CompanyId)
        {
            CourseDetailList retValue = new CourseDetailList();
            
            try
            {
                DataTable _dt = new DataTable("tblCourse");

                SortedList _param = new SortedList();
                _param.Add("@CompanyId", CompanyId);
                _dt = _db.GetDataTableBySP("usp_CourseDetailSelectItemList", _param);

                if (_dt != null && _dt.Rows.Count > 0)
                {
                    CourseDetail _courseDetail = new CourseDetail();
                    foreach(DataRow _dr in _dt.Rows)
                    {
                        FillData(_dr, _courseDetail);
                        retValue.Add(_courseDetail);
                    }
                }
            }
            catch (Exception Ex)
            {
                retValue.Error = new MyError();
                retValue.Error.ErrorNo = 10001;
                retValue.Error.ErrorLocation = "Course";
                retValue.Error.ErrorMessage = Ex.Message;
                retValue.Error.ErrorStack = Ex.StackTrace;

                MyError.CreateExceptionLog(Ex, "Course");
            }

            return retValue;
        }

        public CourseDetail GetCourseDetail(int Id)
        {
            CourseDetail retValue = new CourseDetail();
            
            try
            {
                DataTable _dt = new DataTable("tblCourse");

                SortedList _param = new SortedList();
                _param.Add("@Id", Id);
                _dt = _db.GetDataTableBySP("usp_CourseDetailSelectSingleItem", _param);

                if (_dt != null && _dt.Rows.Count > 0)
                {
                    FillData(_dt.Rows[0], retValue);
                }
            }
            catch (Exception Ex)
            {
                retValue.Error = new MyError();
                retValue.Error.ErrorNo = 10001;
                retValue.Error.ErrorLocation = "Course";
                retValue.Error.ErrorMessage = Ex.Message;
                retValue.Error.ErrorStack = Ex.StackTrace;

                MyError.CreateExceptionLog(Ex, "Course");
            }

            return retValue;
        }

        private void FillData(DataRow aRow, CourseDetail CourseDetail)
        {
            CourseDetail.Error = new MyError();
            CourseDetail.Id = Convert.ToInt32(aRow["Id"].ToString());
            CourseDetail.CompanyID = Convert.ToInt32(aRow["CompanyID"].ToString());
            CourseDetail.CourseID = Convert.ToInt32(aRow["CourseId"].ToString());
            CourseDetail.MajorFieldID = Convert.ToInt32(aRow["MajorFieldID"].ToString());
            CourseDetail.StandardLevelID = Convert.ToInt32(aRow["StandardLevelID"].ToString());

            if (aRow["Description"] != DBNull.Value)
            {
                CourseDetail.Description = aRow["Description"].ToString();
            }
            else
            {
                CourseDetail.Description = string.Empty;
            }

            CourseDetail.Name = aRow["Name"].ToString();
            CourseDetail.CompanyName = aRow["CompanyName"].ToString();
            CourseDetail.StandardLevelName = aRow["StandardLevelName"].ToString();
            CourseDetail.MajorFieldName = aRow["MajorFieldName"].ToString();
            CourseDetail.CourseName = aRow["CourseName"].ToString();

            CourseDetail.IsActive = Convert.ToBoolean(aRow["IsActive"].ToString());

            CourseDetail.Error.ErrorNo = 0;
        }
    }
}
